package com.bootdo.common.elasticsearch;

import java.util.ArrayList;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestClientBuilder.RequestConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
@Component
public class RestHighLevelConfig {
	
//	    private static String hosts = "127.0.0.1"; // 集群地址，多个用,隔开
//	    private static int port = 9200; // 使用的端口号
//		@Value("${es.port}")
//		private int port;
		@Value("${es.host}")
		private String host;
	    private static String schema = "http"; // 使用的协议
	    //private  ArrayList<HttpHost> hostList = null;
	 
	    private static int connectTimeOut = 5000; // 连接超时时间
	    private static int socketTimeOut = 40000; // 连接超时时间
	    private static int connectionRequestTimeOut = 1000; // 获取连接的超时时间
	 
	    private static int maxConnectNum = 100; // 最大连接数
	    private static int maxConnectPerRoute = 100; // 最大路由连接数



	    @Bean
	    public HttpHost[] httpHost(){
	        //解析hostlist配置信息
	        String[] split = host.split(",");
	        //创建HttpHost数组，其中存放es主机和端口的配置信息
	        HttpHost[] httpHostArray = new HttpHost[split.length];
	        for(int i=0;i<split.length;i++){
	            String item = split[i];
	            httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), schema);
	        }
	        return httpHostArray;
	    }

	    @Bean(name = "restHighLevelClient") 
	    public RestHighLevelClient client(){

			 RestClientBuilder builder =   RestClient.builder(httpHost());
	   
//	    	  RestClient.builder(
//                      new HttpHost(
//                              clusterNodes.split(":")[0],
//                              Integer.parseInt(clusterNodes.split(":")[1]),
//                              "http")));
//                              
//	        RestClientBuilder builder = RestClient.builder(hostConfig().toArray(new HttpHost[0]));
	        // 异步httpclient连接延时配置
	        builder.setRequestConfigCallback(new RequestConfigCallback() {
	            @Override
	            public Builder customizeRequestConfig(Builder requestConfigBuilder) {
	                requestConfigBuilder.setConnectTimeout(connectTimeOut);
	                requestConfigBuilder.setSocketTimeout(socketTimeOut);
	                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
	                return requestConfigBuilder;
	            }
	        });
	        // 异步httpclient连接数配置
	        builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
	            @Override
	            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
	                httpClientBuilder.setMaxConnTotal(maxConnectNum);
	                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
	                return httpClientBuilder;
	            }
	        });
	        return new RestHighLevelClient(builder);
	    }
}
